package fr.afcepf.ai85.gepeto.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;

import fr.afcepf.ai85.ent.Eleve;
import fr.afcepf.ai85.ent.MessageEnvoye;
import fr.afcepf.ai85.ent.MessageRecu;
import fr.afcepf.ai85.gepeto.service.ServiceEleveHib;
import fr.afcepf.ai85.gepeto.service.ServiceMessageEnvoye;
import fr.afcepf.ai85.gepeto.service.ServiceMessageRecu;

@ManagedBean(name = "beanMessageRecu")
@SessionScoped
public class BeanMessageRecu {

	private Integer idMessageRecu;
	private List<SelectItem> listeMessageRecu = new ArrayList<SelectItem>();
	private Eleve el;
	private Integer idEd;
	private String message;
	
	@ManagedProperty(value = "#{mbConnexion.leLogin}")
	private String nomLoginConnexion;
	
	private ServiceEleveHib seh;
	
	@PostConstruct
	public void init()
	{
//		System.out.println(nomLoginConnexion);
	seh=new ServiceEleveHib();
	el = seh.rechercherEleveByNomConnexion(nomLoginConnexion);
	idEd=el.getIdEleve();
	System.out.println("BeanMessageRecu init Eleve:"+idEd);
	rechercheMessageRecu(idEd);
	}

	public Integer getIdMessageRecu() {
		return idMessageRecu;
	}

	public void setIdMessageRecu(Integer idMessageRecu) {
		this.idMessageRecu = idMessageRecu;
	}

	public List<SelectItem> getListeMessageRecu() {
		return listeMessageRecu;
	}

	public void setListeMessageRecu(List<SelectItem> listeMessageRecu) {
		this.listeMessageRecu = listeMessageRecu;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNomLoginConnexion() {
		return nomLoginConnexion;
	}

	public void setNomLoginConnexion(String nomLoginConnexion) {
		this.nomLoginConnexion = nomLoginConnexion;
	}
	
	public void rechercheMessageRecu(Integer idEleve){
		
		ServiceMessageRecu sme= new ServiceMessageRecu();
		List<MessageRecu> laListeM=sme.listeMessageRecu(idEleve);
		
		for(MessageRecu me:laListeM){
			SelectItem si = new SelectItem(me.getIdMessageRecu(), me.getObjetMessageRecu());
			listeMessageRecu.add(si);
		}		
//		idMessageEnvoye = Integer.parseInt(listeMessageEnvoye.get(0).getValue().toString());
//		chargementMessageById();
		
		
		
	}
	
	
	public void recupMessage(AjaxBehaviorEvent event) {				
		System.out.println("Dans la methode recupMessage");
		chargementMessageById();
	}
	
	private void chargementMessageById() {
		ServiceMessageRecu sme= new ServiceMessageRecu();
		System.out.println("BeanMessageRecu chargementMessageById idMessageRecu "+ idMessageRecu);
		message=sme.UnMessageRecu(idMessageRecu);
		
	}
	
	
}
